#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   wzzp.py
@Time    :   2024/01/16 16:12:13
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   温州招聘爬虫类
'''

from pyquery import PyQuery as pq
from selenium.webdriver.common.by import By
import sys

sys.path.append("../../")
from reptile import Reptile
from excel import Excel
from reptile import wait_for_element_with_id

import re
import time
import os


class WZZP(Reptile):
    def __init__(self, dataPath:str, driver_path):
        url = "https://www.wzzp.com/person/Login.aspx"
        list_template = "https://www.wzzp.com/zpzhiwei/diqu//?PageNo={}&Key={}"
        sql_table = """
            create table if not exists wzzp(
                id integer,
                jobname varchar(50),
                company varchar(50),
                address varchar(50),
                companyScale varchar(50),
                number varchar(50),
                lim varchar(50),
                date timestamp not null default (datetime('now', 'localtime')),
                UNIQUE (jobname, company),
                primary key (id)
                );
            """
        super().__init__(url, dataPath, list_template, sql_table, driver_path)

    def login(self, username: str, password: str):
        self.web.get(self.url)
        print("切换登录方式")
        wait_for_element_with_id(self.web, "al02")
        time.sleep(1)
        loginTypeElement = self.web.find_element(By.ID,"al02")
        loginTypeElement.click()
        print("登录")
        print("输入用户名")
        usernameElement = self.web.find_element(By.ID,"pwdUsername")
        usernameElement.clear()
        usernameElement.send_keys(username)
        print("输入密码")
        passwordElement = self.web.find_element(By.ID,"pwdPassword")
        passwordElement.clear()
        passwordElement.send_keys(password)
        print("点击登录")
        loginElement = self.web.find_element(By.ID,"btnPWDLogin")
        loginElement.click()
        time.sleep(1)
    
    def getUrl(self, key: str, pages: int):
        self.key = key
        self.message_url_list = []
        for page in range(1, int(pages) + 1):
            url = self.list_template.format(page, key)
            print(url)
            time.sleep(1)
            self.web.get(url)
            element = pq(self.web.page_source)
            self.message_url_list += self.parseUrl(element)

    def run(self):
        return super().run()
    
    def save(self):
        sql = """
        replace 
        into 
        wzzp (
            jobname,
            company,
            address,
            companyScale,
            number,
            lim)
        values(?, ?, ?, ?, ?, ?)
        """
        return super().save(sql)
    
    def close(self):
        return super().close()
    
    def parseUrl(self, element: pq):
        message_url_list = []
        for i in element('.seaList ul').items():
            message_url_list.append("https://www.wzzp.com/" + i.find('.s1 a').attr('href'))
        return message_url_list

    def analysis(self, element: pq):
        dataDict = {}
        dataDict["jobname"] = element('.jobname').text()
        dataDict["company"] = element('.n1 p a').text()
        dataDict["address"] = element('.li4 i').text().replace("(", "").replace(")", "")
        dataDict["companyScale"] = element('.r3').text()
        dataDict["number"] = element('.li5').text()
        dataDict["limit"] = element('.li3').text()
        if len(dataDict) < 6:
            return {}
        
        return dataDict
    
class WZZPTOExcel(Excel):
    def __init__(self, path, db_name, table_name):
        super().__init__(path, db_name, table_name, 1)

    def dataToExcelAll(self):
        return super().dataToExcelAll()
    
    def dataToExcel(self):
        columns = {
            "jobname":"岗位",
            "company":"公司",
            "address":"地址",
            "companyScale":"公司规模",
            "number":"招聘数量",
            "lim":"限制"
            }
        df = self.df.rename(columns=columns)
        excel_file_path = os.path.join(self.path, f"{self.db_name.split('.')[0]}.xlsx")
        df = df[df["招聘数量"].astype(str).str.match(r".*?\d+.*?")]
        df['招聘数量'] = df['招聘数量'].apply(lambda x:int(re.findall(r"\d+",x)[0]))
        df = df[df['招聘数量'] > 20]
        super().excelSave(excel_file_path, df)

if __name__ == "__main__":
    username = "######"
    password = "######"
    loginBtn = "#sub"
    leqing = WZZP(dataPath="wzzp.db")
    leqing.login(username=username, password=password)
    leqing.getUrl(1, "普工")
    leqing.run()
    leqing.save()
    leqing.close()
    to_excel = WZZPTOExcel("./", "pugong.db", "wzzp")
    to_excel.dataToExcel()
    to_excel.dataToExcelAll()